package sm.upgrade;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sm.upgrade.api.UpgradePhase;
import sm.util.SMMap;
import sm.util.Util;

public class FixInboxDataUpgrade implements UpgradePhase {

	private static Logger logger = LoggerFactory.getLogger(UpgradePhase.class);

	private HibernateTemplate template;

	public void setTemplate(HibernateTemplate template) {
		this.template = template;
	}

	@Override
	public void upgrade() {
		logger.info("Start fixing inbox query.");

		List<Object> inboxes = template.find("from inbox");
		for (Object inbox : inboxes) {
			SMMap inboxMap = (SMMap) inbox;
			Object shortQuery = inboxMap.get("short_query");
			Object query = inboxMap.get("query");
			if (!Util.equals(shortQuery, query)) {
				inboxMap.put("short_query", query);
				template.update("inbox", inboxMap);
			}
		}

		logger.info("  Finish fixing inbox query.");

	}

}
